/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.java.tools;

import com.jb.framework.util.BeanUtil;
import com.jb.util.ClassUtil;
import com.jb.web.bean.event.UIEventHandler;

public class InterfaceRunner extends AbstractCodeGenerator implements Runner {
    Class mInterface; //= ;//EventHandler.class;

    public InterfaceRunner(String pPackageName, String pClassName, Class interfaceClass) {
        super(pPackageName, pClassName);
        mInterface = interfaceClass;
    }

    //interface needed for CodeGenerator
    public String getCode() {
        StringBuffer code = new StringBuffer();

        if ((getPackageName() != null) && (getPackageName().length() != 0)) {
            code.append("package " + getPackageName() + ";\n");
        }

        code.append("/** \n").append("* This code is generated by InterfaceRunner, please do not change it manually \n")
            .append("**/\n").append("public class " + getClassName() + " implements " + getInterface().getName() +
            "\n").append("{").append("\n\tpublic void execute() \n\t{")
            .append("\n\t\tSystem.out.println(\"" + System.currentTimeMillis() + " \");\n").append("\t}\n}\n");

        return code.toString();
    }

    public Class getInterface() {
        return mInterface;
    }

    //interface needed for CodeGenerator
    public Runner getRunner() {
        return this;
    }

    //interface of runner
    public void execute() {
        UIEventHandler eventHandler = (UIEventHandler) ClassUtil.createInstance(getClassQName());
        if(eventHandler!=null)
            eventHandler.handleEvent(null, null);
    }
}
